package com.doge.walknovel.business.reader.comments

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.doge.wnpro.R
import com.doge.walknovel.api.model.CommentInfo
import com.doge.walknovel.business.reader.menus.ReaderHelper
import com.doge.walknovel.core.base.adapters.AdapterCallback
import com.doge.wnpro.databinding.CellReaderCommentsBinding
import com.doge.walknovel.utils.*

class ReaderCommentsViewHolder(
    parent: ViewGroup,
    callback: AdapterCallback,
    private val binding: CellReaderCommentsBinding = CellReaderCommentsBinding.inflate(
        LayoutInflater.from(parent.context), parent, false),
) : RecyclerView.ViewHolder(binding.root) {
    private val light by lazy { ReaderHelper.light }

    init {
        binding.collect.setOnClickCallback {
            callback.invoke(COMMENTS_COLLECT, itemView.tag)
        }
        binding.more.setOnClickCallback {
            callback.invoke(COMMENTS_REPLY, itemView.tag)
        }

    }

    fun bind(item: CommentInfo) {
        itemView.tag =
            updateByLight()
        binding.avatar.load(
            item.profilePicture,
            R.mipmap.comments_avatar_default,
            transformation = CircleCrop,
            width = 36.dp, height = 36.dp
        )
        binding.nickname.text = item.nickName
        binding.timestamp.text = item.commentTime?.timeDesc
        binding.content.text = item.content
        binding.collectImage.setImageResource(if (item.likeFlag == 1) R.mipmap.comments_collect_light else R.mipmap.comments_collect_normal)
        binding.collectValue.text = (item.likeCount ?: 0).toString()
    }


    private fun updateByLight() {
        if (light) {
            binding.commentBg.setBackgroundResource(R.drawable.shape_f7f7f7_16)
            binding.nickname.setTextColor(getColor("#333333"))
            binding.timestamp.setTextColor(getColor("#666666"))
            binding.content.setTextColor(getColor("#333333"))
        } else {
            binding.commentBg.setBackgroundResource(R.drawable.shape_222222_16)
            binding.nickname.setTextColor(getColor("#FFFFFF"))
            binding.timestamp.setTextColor(getColor("#FFFFFF"))
            binding.content.setTextColor(getColor("#FFFFFF"))
        }
    }
}